package com.A21.GuoSai.demo.workflow_adjust.service;

import com.A21.GuoSai.demo.atom_adjust.service.AtomPojoService;
import com.A21.GuoSai.demo.service_entity.annotation.Author;
import com.A21.GuoSai.demo.service_entity.entity.atom.AtomServicePojo;
import com.A21.GuoSai.demo.service_entity.entity.workflow.StaticVarValuePojo;
import com.A21.GuoSai.demo.service_entity.entity.workflow.WorkFlowPojo;
import com.A21.GuoSai.demo.service_entity.utils.BpmnUtils;
import io.camunda.zeebe.model.bpmn.BpmnModelInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class testB {

    @Autowired
    private WorkFlowPojoService workFlowPojoService;
    @Autowired
    private ZeebeDeployService deployService;
    @Autowired
    private AtomPojoService atomService;

    @Test
    public void test() throws Exception {
//        zebeDeployService.deleteWorkFlow("123456");
        WorkFlowPojo workFlowMap = new WorkFlowPojo();
        workFlowMap.setFlowId("my345678");
        workFlowMap.setFlowDesc("my345678");
        workFlowMap.setFlowName("my345678");
        workFlowMap.setCreateDate(new Date());
        workFlowMap.setCreator("xiaofan");

        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("processes/my3-1.bpmn");
        BufferedReader reader = new BufferedReader(new InputStreamReader(resourceAsStream));
        String bpmn = "";
        String readmess;
        while ((readmess = reader.readLine()) != null) {
            bpmn += readmess;
        }
        workFlowMap.setBpmn(bpmn);

        workFlowPojoService.saveWorkerFlow(workFlowMap);
    }

    @Test
    public void test02() throws Exception {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("processes/my3.bpmn");
        BufferedReader reader = new BufferedReader(new InputStreamReader(resourceAsStream));
        String bpmn = "";
        String readmess;
        while ((readmess = reader.readLine()) != null) {
            bpmn += readmess;
        }
        BpmnModelInstance modelInstance = BpmnUtils.getModelInstance(bpmn);
        workFlowPojoService.insertAllVariblesFromBpmn(modelInstance, "123456");
    }

    @Test
    public void test03() {
        Map<String, Object> map = new HashMap<>();
//        map.put("a1",20);
//        map.put("b1","str1");
        deployService.executeWorkFlow("my345678", map);
    }

    @Test
    public void test04(){
        List<AtomServicePojo> workerFlowStaVarByFlowId = workFlowPojoService.getWorkerFlowStaVarByFlowId("234567");
        workerFlowStaVarByFlowId.forEach(
                atom->{
                    List<StaticVarValuePojo> staticVarList = atom.getStaticVarList();
                    staticVarList.forEach(staticVarValuePojo -> {
                        System.out.println(staticVarValuePojo.toString());
                    });
                }
        );
    }


    @Test
    public void test05(){
        List<AtomServicePojo> workerFlowStaVarByFlowId = workFlowPojoService.getWorkerFlowStaVarByFlowId("234567");
        List<AtomServicePojo> allAtomMessages = atomService.getAllAtomMessages();
        allAtomMessages.forEach(
                atom->{
                    System.out.println(atom.toString());
                }
        );
    }
}



class TestC{
    @Author(author = "kk",attributeConnnect = "dd")
    public void name(){
        Method[] declaredAnnotations = this.getClass().getMethods();
    }


    @Test
    public void test06() {
        System.out.println(this);
        this.name();
    }
}